log = require '../log'
config = require '../config'

EntityService = require './EntityService'

SubAppBackend = "backend"

exports.gInit = ->
    ctx = {}

    subApp = yield from EntityService.gFindOneByCriteria(ctx, null, 'F_SubApp', {name: SubAppBackend})
    unless subApp
        log.system.info 'Create sub app: backend'
        yield from EntityService.gCreate(ctx, 'F_SubApp', {
            name: SubAppBackend,
            usernameFields: ["username", "phone", "email"]
        })

    initServers = config.initServers
    if initServers and initServers.length
        for s in initServers
            server = yield from EntityService.gFindOneByCriteria(ctx, null, 'F_Server', {host: s.host})
            unless server
                log.system.info 'Create init server: ' + s.host
                yield from EntityService.gCreate(ctx, 'F_Server', {host: s.host, app: s.app})